package com.alks.function.service.impl.finance.salary;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.excel.write.metadata.fill.FillWrapper;
import com.alks.common.aop.AutoPageAop;
import com.alks.common.config.handler.AutoCellWriteWidthHandle;
import com.alks.common.data.thread.UserIdThread;
import com.alks.common.service.RedisService;
import com.alks.common.utils.DateUtils;
import com.alks.common.utils.UserInfoUtil;
import com.alks.common.utils.beanutils.BeanUtil;
import com.alks.common.utils.excelUtils.ExcelPoiUtils;
import com.alks.common.utils.excelUtils.ExcelUtils;
import com.alks.common.utils.stringUtils.ZStringUtils;
import com.alks.entity.data.entity.PcCostDetail2;
import com.alks.entity.data.entity.PcCostTemp;
import com.alks.entity.data.entity.PcCostType;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.entity.exception.ServiceErrorException;
import com.alks.function.config.ProfileConfig;
import com.alks.function.data.dto.finance.salary.PcCostDetail2Dto;
import com.alks.function.data.dto.finance.salary.PcCostTypeDto;
import com.alks.function.data.request.finance.salary.PcCostDetail2AddRequest;
import com.alks.function.data.request.finance.salary.PcCostDetail2Request;
import com.alks.function.data.request.finance.salary.PcCostDetail2UpdateRequest;
import com.alks.function.data.request.finance.salary.PcCostTypeRequest;
import com.alks.function.mapper.administration.PcEmployeeMapper;
import com.alks.function.mapper.finance.ryfinance.PcCostTempMapper;
import com.alks.function.mapper.finance.salary.SubsidyDeductionMapper;
import com.alks.function.service.administration.PcEmpAttendService;
import com.alks.function.service.finance.salary.SubsidyDeductionService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.alks.entity.data.enums.RedisKeyEnum.COST_DETAIL2;

@Service
@Slf4j
public class SubsidyDeductionServiceImpl implements SubsidyDeductionService {

    @Resource
    private SubsidyDeductionMapper subsidyDeductionMapper;

    @Resource
    private PcCostTempMapper pcCostTempMapper;

    @Resource
    private RedisService redisService;

    @Resource
    private PcEmployeeMapper pcEmployeeMapper;

    @Resource
    private PcEmpAttendService pcEmpAttendService;

    /**
     * 扣款明细查询
     *
     * @param request
     * @return
     */
    @Override
    @AutoPageAop
    public ResponseInfo SubsidyDeductionQuery(PcCostDetail2Request request) {
        Map map = new HashMap<>();
        List<PcCostDetail2Dto> dots = subsidyDeductionMapper.getPcCostDetail2List(request);
        Double qtyCount = subsidyDeductionMapper.getQtyCount(request);
        Double amtCount = subsidyDeductionMapper.getAmtCount(request);
        PageInfo pageInfo = new PageInfo(dots);
        map.put("list", dots);
        map.put("total", pageInfo.getTotal());
        map.put("qtyCount", qtyCount);
        map.put("amtCount", amtCount);
        return ResponseInfo.ok(map);
    }

    /**
     * 检核作业-0审核/1取消
     *
     * @param request
     * @param choose
     * @return
     */
    @Override
    public ResponseInfo InspectionWork(PcCostDetail2Request request, Integer choose) {
        if (choose==0) {
            if (ObjectUtils.isEmpty(request.getFinanceYymm2())) {
                return ResponseInfo.error("请输入结算月份!");
            }
            boolean yearMonth = DateUtils.isValidYearMonth(request.getFinanceYymm2());
            if (!yearMonth) {
                return ResponseInfo.error("请输入正确的结算月份!");
            }
        }
        List<PcCostDetail2Dto> dots = subsidyDeductionMapper.getPcCostDetail2List(request);
        if (ObjectUtils.isEmpty(dots)) {
            return ResponseInfo.error("查询记录为空不可结算!");
        }
        int count = 0;
        int balanceCount = 0;
        for (PcCostDetail2Dto t : dots) {
            if ("Y".equals(t.getCfmFlag())){
                count++;
            }
            if(!ObjectUtils.isEmpty(t.getBalanceNo())){
                balanceCount++;
            }
        }
        List<Integer> ids = dots.stream().map(t -> t.getId()).collect(Collectors.toList());
        String userName = UserIdThread.get().getUserName();
        switch (choose) {
            case 0:
                if (count > 0) {
                    return ResponseInfo.error("存在"+count+"条已结算记录,不可重复结算!");
                }
                subsidyDeductionMapper.InspectionWork(ids, userName, LocalDateTime.now(), "Y", request.getFinanceYymm2());
                break;
            case 1:
                if (balanceCount > 0) {
                    return ResponseInfo.error("存在"+balanceCount+"条已结算已应付的记录,不可取消结算!");
                }
                subsidyDeductionMapper.InspectionWork(ids, null, null, null, null);
                break;
            default:
                return ResponseInfo.error("请选择结算/取消结算!");
        }
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 扣款/补贴定义方式-查询
     *
     * @param flag
     * @return
     */
    @Override
    public ResponseInfo DeductionDefinition(Integer flag) {
        String companyId = UserIdThread.get().getCompanyId();
        List<PcCostTypeDto> list = subsidyDeductionMapper.getPcCostType(flag, companyId);
        return ResponseInfo.ok(list);
    }

    /**
     * 扣款/补贴定义方式-新增
     *
     * @param request
     * @return
     */
    @Override
    public ResponseInfo pcCostTypeAdd(PcCostTypeRequest request) {
        if (ObjectUtils.isEmpty(request.getTypeNo())) {
            return ResponseInfo.error("请输入类型编码!");
        }
        if (ObjectUtils.isEmpty(request.getTypeName())) {
            return ResponseInfo.error("请输入类型名称!");
        }
        UserInfoUtil.UserInfo userInfo = UserInfoUtil.getUserInfo();
        String typeNo = subsidyDeductionMapper.getPcCostTypeNo(userInfo.getCompanyId(), request.getTypeNo());
        if (ObjectUtils.isNotEmpty(typeNo)) {
            return ResponseInfo.error("该编码已存在,不能重复!");
        }
        String typeName = subsidyDeductionMapper.getPcCostTypeName(userInfo.getCompanyId(), request.getTypeName());
        if (ObjectUtils.isNotEmpty(typeName)) {
            return ResponseInfo.error("该类型已存在,不能重复!");
        }
        PcCostType pcCostType = new PcCostType();
        BeanUtil.copyProperties(request, pcCostType);
        pcCostType.setCompanyId(userInfo.getCompanyId());
        pcCostType.setSysUser(userInfo.getUserName());
        pcCostType.setSysDate(LocalDateTime.now());
        subsidyDeductionMapper.addPcCostType(pcCostType);
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 扣款/补贴定义方式-删除
     *
     * @param request
     * @return
     */
    @Override
    public ResponseInfo pcCostTypeDel(PcCostTypeRequest request) {
        if (ObjectUtils.isEmpty(request.getTypeNo())) {
            return ResponseInfo.error("类型编码不能为空!");
        }
        subsidyDeductionMapper.delPcCostType(request.getTypeNo(), UserIdThread.get().getCompanyId());
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 员工列表
     *
     * @return
     */
    @Override
    public ResponseInfo getEmployeeList() {
        return ResponseInfo.ok(subsidyDeductionMapper.geEmployeeList());
    }

    /**
     * 扣款明细增加
     *
     * @param request
     * @return
     */
    @Override
    @Transactional
    public ResponseInfo PcCostDetail2Add(PcCostDetail2AddRequest request) {
        toDetailReqBlankCheck(request);
        UserInfoUtil.UserInfo userInfo = UserInfoUtil.getUserInfo();
        LambdaQueryWrapper<PcCostTemp> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PcCostTemp::getCompanyId, userInfo.getCompanyId())
                .eq(PcCostTemp::getSysUser, userInfo.getLoginName());
        List<PcCostTemp> temps = pcCostTempMapper.selectList(wrapper);
        if (ObjectUtils.isEmpty(temps)) {
            return ResponseInfo.error("扣款明细资料为空!");
        }
        String slipNo = temps.stream()
                .filter(a -> "修改".equals(a.getFeeStatus()))
                .map(PcCostTemp::getFeeSlipNo).findAny()
                .orElse(redisService.slipNoCreat(COST_DETAIL2.getKey()));
        List<PcCostDetail2> details = BeanUtil.copyList(temps, PcCostDetail2.class);
        List<PcCostTypeDto> pcCostType = subsidyDeductionMapper.getPcCostType(3, userInfo.getCompanyId());
        int flag = getFlag(request.getTypeName(), pcCostType);
        for (PcCostDetail2 detail : details) {
            detail.setCompanyId(userInfo.getCompanyId());
            detail.setSysUser(userInfo.getUserName());
            detail.setSysDate(LocalDateTime.now());
            detail.setSlipDate(request.getSlipDate());
            detail.setSlipNo(slipNo);
            detail.setDeptNo(request.getDeptNo());
            detail.setVendorName(request.getVendorName());
            detail.setVendorNo(request.getVendorNo());
            detail.setTypeName(request.getTypeName());
            detail.setTypeNo(request.getTypeNo());
            detail.setAccountName(request.getAccountName());
            detail.setSendNo(request.getSendNo());
            BigDecimal amount = detail.getAmt();
            if (flag == 0 && amount != null) {
                if (amount.compareTo(BigDecimal.ZERO) > 0) {
                    amount = amount.negate();
                }
            }else if (flag == 1 && amount != null){
                if (amount.compareTo(BigDecimal.ZERO) < 0) {
                    amount = amount.negate();
                }
            }
            detail.setAmt(amount);
        }
        subsidyDeductionMapper.addPcCostDetail2(details);
        pcCostTempMapper.delete(wrapper);
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 入参判断
     *
     * @param request
     */
    private void toDetailReqBlankCheck(PcCostDetail2AddRequest request) {
        if (request.getSlipDate() == null) {
            throw new ServiceErrorException("单据日期不能为空");
        }
        if (ObjectUtils.isEmpty(request.getDeptNo())) {
            throw new ServiceErrorException("部门不能为空");
        }
        if (ObjectUtils.isEmpty(request.getVendorName())) {
            throw new ServiceErrorException("员工不能为空");
        }
        if (ObjectUtils.isEmpty(request.getTypeName())) {
            throw new ServiceErrorException("扣款资料类型不能为空");
        }
        if (ObjectUtils.isEmpty(request.getAccountName())) {
            throw new ServiceErrorException("结算方式不能为空");
        }
    }

    /**
     * 扣款明细修改
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public ResponseInfo PcCostDetail2Update(String id) {
        if (ObjectUtils.isEmpty(id)) {
            return ResponseInfo.error("参数不能为空!");
        }
        PcCostDetail2Dto pcCostDetail2 = subsidyDeductionMapper.getPcCostDetail2(id);
        if (ObjectUtils.isEmpty(pcCostDetail2)) {
            return ResponseInfo.error("查询数据异常!");
        }
        if ("Y".equals(pcCostDetail2.getCfmFlag())){
            return ResponseInfo.error("已结算,不能修改!");
        }
        PcCostTemp temp = BeanUtil.copyProperties(pcCostDetail2, PcCostTemp.class);
        UserInfoUtil.UserInfo userInfo = UserInfoUtil.getUserInfo();
        LambdaQueryWrapper<PcCostTemp> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PcCostTemp::getCompanyId, userInfo.getCompanyId())
                .eq(PcCostTemp::getSysUser, userInfo.getLoginName());
        pcCostTempMapper.delete(wrapper);
        temp.setCompanyId(userInfo.getCompanyId());
        temp.setSysUser(userInfo.getLoginName());
        temp.setFeeItem(temp.getItem());
        temp.setFeeSlipNo(pcCostDetail2.getSlipNo());
        temp.setFeeStatus("修改");
        pcCostTempMapper.insert(temp);
        subsidyDeductionMapper.delPcCostDetail2(id);
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 删除扣款明细
     *
     * @param ids
     * @return
     */
    @Override
    public ResponseInfo PcCostDetail2Del(List<Integer> ids) {
        if (ObjectUtils.isEmpty(ids)) {
            return ResponseInfo.error("请选择记录!");
        }
        subsidyDeductionMapper.delPcCostDetail2ToId(ids);
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 修改结算方式
     *
     * @param list
     * @param choose
     * @return
     */
    @Override
    public ResponseInfo PcCostDetail2Update2(List<PcCostDetail2UpdateRequest> list, Integer choose) {
        if (ObjectUtils.isEmpty(list)) {
            return ResponseInfo.error("请选择记录!");
        }
        switch (choose) {
            case 1:
                getAccountNameCount(list, "喜得狼");
                subsidyDeductionMapper.updatePcCostDetail2(list, "喜得狼");
                break;
            case 2:
                getAccountNameCount(list, "荣阳");
                subsidyDeductionMapper.updatePcCostDetail2(list, "荣阳");
                break;
            default:
                return ResponseInfo.error("结算方式不能为空!");
        }
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 比较记录是否一致
     *
     * @param list
     * @param accountName
     */
    private void getAccountNameCount(List<PcCostDetail2UpdateRequest> list, String accountName) {
        int count = 0;
        int userCount = 0;
        for (PcCostDetail2UpdateRequest t : list) {
            if (accountName.equals(t.getAccountName())) {
                count++;
            }
            if (UserIdThread.get().getUserName().equals(t.getSysUser())){
                userCount++;
            }
        }
        if (count > 0) {
            throw new ServiceErrorException(count + "条记录结算方式为空或一致,不能修改!");
        }
        if (count > 0) {
            throw new ServiceErrorException(userCount + "条记录不是本人的单据,不能修改!");
        }
    }

    /**
     * 扣款明细导出
     * @param request
     * @param response
     */
    @Override
    public void PcCostDetail2Export(PcCostDetail2Request request, HttpServletResponse response) {
        List<PcCostDetail2Dto> list = subsidyDeductionMapper.getPcCostDetail2List(request);
        String fileName = "扣款明细" + DateUtils.dateTimeNow() + ".xls";
        if (ZStringUtils.isEmpty(list)) {
            throw new ServiceErrorException("查询数据为空!");
        }
        BigDecimal count = BigDecimal.ZERO;
        for (PcCostDetail2Dto t : list) {
            if (t.getPrice() != null) {
                count = count.add(t.getPrice());
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("count", count);
        try (
                InputStream inputStream = new ClassPathResource("templates/扣款明细模板.xls").getInputStream();
                OutputStream outputStream = response.getOutputStream();
                ExcelWriter excelWriter = EasyExcel.write(outputStream).excelType(ExcelTypeEnum.XLS).withTemplate(inputStream).registerWriteHandler(new AutoCellWriteWidthHandle()).build();
        ) {
            ExcelUtils.setResponseZF(response, fileName);
            WriteSheet writeSheet = EasyExcel.writerSheet().build();
            FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
            excelWriter.fill(map, fillConfig, writeSheet);
            excelWriter.fill(new FillWrapper("t", list), fillConfig, writeSheet);
            excelWriter.finish();
        } catch (Exception e) {
            log.error("扣款明细导出失败, 错误信息: {}", e.getMessage(), e);
            throw new ServiceErrorException("扣款明细导出失败!");
        }
    }

    /**
     * 导入扣款资料
     * @param file
     * @param request
     * @return
     */
    @Override
    public ResponseInfo PcCostDetail2Import(MultipartFile file, PcCostDetail2AddRequest request) {
        if (ObjectUtils.isEmpty(request.getDeptNo())) {
            throw new ServiceErrorException("部门不能为空");
        }
        if (ObjectUtils.isEmpty(request.getTypeName())) {
            throw new ServiceErrorException("扣款资料类型不能为空");
        }
        if (ObjectUtils.isEmpty(request.getAccountName())) {
            throw new ServiceErrorException("结算方式不能为空");
        }
        UserInfoUtil.UserInfo userInfo = UserInfoUtil.getUserInfo();
        //是否补贴项目
        List<PcCostTypeDto> pcCostType = subsidyDeductionMapper.getPcCostType(3, userInfo.getCompanyId());
        int flag = getFlag(request.getTypeName(), pcCostType);
        List<PcCostDetail2> list = excelInput(file,flag);
        if (ObjectUtils.isEmpty(list)){
            throw new ServiceErrorException("请选择正确的文件格式/数据!");
        }
        List<PcCostDetail2> detail2s = list.stream().
                filter(pcCostDetail2 -> pcCostDetail2.getVendorNo() != null).collect(Collectors.toList());
        if(list.size() != detail2s.size()){
            throw new ServiceErrorException("导入文件存在工号为空的数据,请检查!");
        }
        Function<PcCostDetail2, AbstractMap.SimpleEntry<String, String>> compositeKeyMapper =
                pcCostDetail2 -> new AbstractMap.SimpleEntry<>(pcCostDetail2.getVendorNo(), pcCostDetail2.getCostDesc());

        Map<AbstractMap.SimpleEntry<String, String>, List<PcCostDetail2>> groupedByCompositeKey =
                detail2s.stream()
                        .collect(Collectors.groupingBy(compositeKeyMapper));

        // 遍历分组结果，检查是否有重复数据
        for (Map.Entry<AbstractMap.SimpleEntry<String, String>, List<PcCostDetail2>> entry : groupedByCompositeKey.entrySet()) {
            List<PcCostDetail2> dataList = entry.getValue();
            if (dataList.size() > 1) {
                throw new ServiceErrorException(
                        String.format("导入文件存在相同的数据: 工号=%s, 费用描述=%s,导入失败,请检查数据是否有重复!",
                                entry.getKey().getKey(), entry.getKey().getValue()));
            }
        }
        if (!ObjectUtils.isEmpty(detail2s)) {
            String slipNo = redisService.slipNoCreat(COST_DETAIL2.getKey());
            int item=1;
            for (PcCostDetail2 t : detail2s) {
                String deptName = pcEmployeeMapper.getEmployeeByWorkerId(t.getVendorNo(), t.getVendorName());
                if (ObjectUtils.isEmpty(deptName)){
                    return ResponseInfo.error("员工:"+t.getVendorNo()+",工号:"+t.getVendorName()+",不在人事档案中存在,请检查导入文件!");
                }
                t.setCompanyId(userInfo.getCompanyId());
                t.setDeptNo(request.getDeptNo());
                t.setTypeNo(request.getTypeNo());
                t.setTypeName(request.getTypeName());
                t.setSendNo(request.getSendNo());
                t.setAccountName(request.getAccountName());
                t.setSysUser(userInfo.getUserName());
                t.setSysDate(LocalDateTime.now());
                t.setSlipNo(slipNo);
                if (flag==1){
                   t.setProcessStatus("补贴");
                }else {
                    t.setProcessStatus("扣款");
                }
                t.setItem(BigDecimal.valueOf(item));
                item++;
            }
            subsidyDeductionMapper.addPcCostDetail2(detail2s);
        }
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 判断是否补贴项目
     * @param typeName
     * @param pcCostType
     * @return
     */
    private int getFlag(String typeName, List<PcCostTypeDto> pcCostType) {
        boolean typeFlag = false;
        for (PcCostTypeDto t : pcCostType) {
            if (typeName.equals(t.getTypeName())){
                typeFlag = true;
                break;
            }
        }
        //0扣款,1补贴
        if (typeFlag){
            return 1;
        }else {
            return 0;
        }
    }

    /**
     * 读取excel表数据
     * @param excelFile
     * @param flag
     * @return
     */
    public List<PcCostDetail2> excelInput(MultipartFile excelFile,int flag) {
        String excelFileName = excelFile.getOriginalFilename();
        int excelVersion = ExcelPoiUtils.getExcelVersion(excelFileName);
        Workbook workbook = null;
        List<PcCostDetail2> list = null;

        // 2003版本xls
        if (excelVersion == 1) {
            try {
                log.info("导入文件名:{},文件版本:2003",excelFileName);
                workbook = new HSSFWorkbook(excelFile.getInputStream());
                list = readExcelValue(workbook,flag);
                return list;
            } catch (IOException e) {
                log.error("文件导入失败!");
                e.printStackTrace();
            }
        }
        // 2007版本xlsx
        if (excelVersion == 2) {
            try {
                log.info("导入文件名:{},文件版本:2007",excelFileName);
                workbook = new XSSFWorkbook(excelFile.getInputStream());
                list = readExcelValue(workbook,flag);
                return list;
            } catch (IOException e) {
                log.error("文件导入失败!");
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 处理excel表数据
     * @param workbook
     * @param flag
     * @return
     */
    private List<PcCostDetail2> readExcelValue(Workbook workbook,int flag) {
        int totalRows = 0;
        int totalCells = 0;
        // 获取第一张表
        Sheet sheet = workbook.getSheetAt(0);
        // 得到表的总行数
        totalRows = sheet.getPhysicalNumberOfRows();
        // 根据第一行（标题行）获得总列数，获得总列数得根据某行确定
        if (totalRows >= 1 && sheet.getRow(0) != null) {
            totalCells = sheet.getRow(0).getPhysicalNumberOfCells();
        }
        List<PcCostDetail2> list = new ArrayList<>();
        // 逐行取数据，每行对应一条实体对象信息
        for (int rowNum = 2; rowNum < totalRows; rowNum++) {
            // 跳过标题行
            Row row = sheet.getRow(rowNum);
            if (row == null) {
                continue;
            }
            PcCostDetail2 vo = new PcCostDetail2();
            // 用于统计空字段的数量
            int emptyFieldCount = 0;
            // 逐列取数据，每列对应一个实体对象属性
            for (int colNum = 0; colNum < totalCells; colNum++) {
                Cell cell = row.getCell(colNum);
                if (null != cell) {
                    switch (cell.getCellType()) {
                        case NUMERIC:
                            if (colNum == 4){
                                BigDecimal amount = BigDecimal.valueOf(cell.getNumericCellValue());
                                if (flag == 0 && amount != null) {
                                    if (amount.compareTo(BigDecimal.ZERO) > 0) {
                                        amount = amount.negate();
                                    }
                                }else if (flag == 1 && amount != null){
                                    if (amount.compareTo(BigDecimal.ZERO) < 0) {
                                        amount = amount.negate();
                                    }
                                }
                                vo.setAmt(amount);
                            }
                            if (colNum == 5){
                                vo.setRemark(String.valueOf(cell.getNumericCellValue()));
                            }
                            break;
                        case STRING:
                            if (colNum == 0){
                                Date date = DateUtils.parseDate(cell.getStringCellValue());
                                Instant instant = date.toInstant();
                                ZonedDateTime zonedDateTime = instant.atZone(ZoneId.of("Asia/Shanghai"));
                                vo.setSlipDate(zonedDateTime.toLocalDateTime());
                            }
                            if (colNum == 1){
                                vo.setVendorNo(cell.getStringCellValue());
                            }
                            if (colNum == 2){
                                vo.setVendorName(cell.getStringCellValue());
                            }
                            if (colNum == 3){
                                vo.setCostDesc(cell.getStringCellValue());
                            }
                            if (colNum == 5){
                                vo.setRemark(cell.getStringCellValue());
                            }
                            break;
                        case BOOLEAN:
                            break;
                        case FORMULA:
                            break;
                        case BLANK:
                            break;
                        default:
                    }
                }
            }
            if (ObjectUtils.isEmpty(vo.getVendorNo())){
                emptyFieldCount++;
            }
            if (ObjectUtils.isEmpty(vo.getVendorName())){
                emptyFieldCount++;
            }
            if (ObjectUtils.isEmpty(vo.getSlipDate())){
                emptyFieldCount++;
            }
            if (ObjectUtils.isEmpty(vo.getCostDesc())){
                emptyFieldCount++;
            }
            if (ObjectUtils.isEmpty(vo.getAmt())){
                emptyFieldCount++;
            }
            if (emptyFieldCount >=5){
                continue;
            }
            list.add(vo);
        }
        return list;
    }

    /**
     * 修改备注信息
     * @param remarkCw
     * @param id
     * @return
     */
    @Override
    public ResponseInfo PcCostDetail2UpdateRemarkCw(String remarkCw, Integer id) {
        if (id==null){
            return ResponseInfo.error("id不能为空");
        }
        subsidyDeductionMapper.updatePcCostDetail2ToRemarkCw(remarkCw,id);
        return ResponseInfo.ok("操作成功!");
    }

    @Resource
    private ProfileConfig  profileConfig;

    @Override
    public ResponseInfo attendToSubsidyDeductionVNM(String yearMonth) {
        if (!profileConfig.isVNMProfile()) {
            return ResponseInfo.error("当前环境不允许执行!");
        }
        // TODO: 2025/6/4 越南考勤自动生成补贴扣款
        // 取当月有打卡记录的日期数据
        // 取当月的公休日数据
        // 判断非公休日是否存在无打卡记录的日期，如果存在（未读取到当天的考勤数据）提示，不允许操作
        return null;
    }
}
